<?php

include_once 'Answer_Patterns.php';
include_once 'Mount_DOM_tree.php';

class Refactoring_Form_Table_Div extends Answer_Patterns {

   public function start($domDocument) {
      $result = "";
      $mount_tree = new Mount_DOM_tree();

      $search = $domDocument->getElementsByTagName('form');

      foreach ($search as $f) {
         $tables = $f->getElementsByTagName('table');
         foreach ($tables as $t_form) {
            $inputs = $t_form->getElementsByTagName('input');
//            echo "teste";
            if ($inputs->length >= 1) {
               $answer = '&LTdiv&GT ; &LTlabel&GT'  ;
               $answer_HTML = '&LTform&GT<br>
                              &nbsp;&nbsp;&LTdiv&GT<br>
                                 &nbsp;&nbsp;&nbsp;&nbsp;&LTdiv&GT<br>
                                 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&LTlabel&GT'.($inputs->item(0)->getAttribute('name') ? $inputs->item(0)->getAttribute('name') .":" : "Identidifação do Elemento: " ).'&LT/label&GT<br>
                                 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&LTdiv&GT&LTinput type="'.$inputs->item(0)->getAttribute('type').'" id="'.$inputs->item(0)->getAttribute('id').'" name="'.$inputs->item(0)->getAttribute('name').'"/&GT&LT/div&GT<br>
                              &nbsp;&nbsp;&nbsp;&nbsp;&LT/div&GT<br>
                              &nbsp;&nbsp;&nbsp;&nbsp;...<br>
                              &nbsp;&nbsp;&LT/div&GT<br>
                               
                           &LT/form&GT';
               
               $result .= parent::elementAnswerPatterns("&LT;$f->nodeName&GT;", $f->getLineNo(), $answer, "remova a tebela substitiundo por ", " e contém uma <strong>&LT;table&GT;</strong> que estrutura o formulário");
            $result .= parent::tableAnswer($mount_tree->structHTML($f, 4, 0), $answer_HTML);
            }
         }
      }

      if (!empty($result)) {
         $result = parent::titleAnswerPatterns("Substituição de tabelas dentro de Forms") . $result;
      }

      return $result;
   }

}
